import style from "./style.module.css";
import {Props,State} from "./model";
import { Link } from "react-router-dom";
import { Component } from "react";
import { Modal } from "antd";
import axios from "axios";
import { setToken, ssoLogin } from "@/utils/login/ssoLogin";
import { ssoLoginType } from "@/utils/login/model";
import store from "@/store";
export default class PolicyCard extends Component<Props,State>{
    state: Readonly<State>={
        isVisible:false
    };




    IwantApply=async(e:any)=>{
        e.stopPropagation();
        let {personal,enterprise,id}=this.props.policyItem;

        if(personal && enterprise){
            // 弹窗
            this.setState({
                isVisible:true
            })

        }else if(personal && !enterprise){
            // 个人登录
            store.dispatch({type:"userChoose",content:"personal"})

                e.stopPropagation();
                if(localStorage.getItem("personalToken")){
                    this.props.history.push({
                        pathname:"apply_policy",
                        state:{
                            policy_id:id,
                            role:"legal"
                        }
                    })
                }else{
                    ssoLogin(ssoLoginType.personal,"http://my.andbridge.com:3000/apply_policy");
                }
                
            
        }else if(enterprise && !personal){
            // 企业
            store.dispatch({type:"userChoose",content:"legal"})
            e.stopPropagation();
            try {
             store.dispatch({type:"userChoose",content:"legal"})
             if(localStorage.getItem("legalToken")){
                 console.log(111)
                 this.props.history.push({
                     pathname:"apply_policy",
                     state:{
                         policy_id:id,
                         role:"personal"
                     }
                 })
             }else{
                 ssoLogin(ssoLoginType.legal)
             }
            } catch (error) {
             console.log(error)
            }
        }else{
            // 点不动
        }

    }

    toPolicyGuideLines=(e:any)=>{
        try {
            e.stopPropagation();
            this.props.history.push({pathname:"/policy_guidelines",state:this.props.policyItem.id})
        } catch (error) {
            console.log(error)
        }
    }
    render = () => {
        const {policyItem}=this.props;
        let {isVisible}=this.state;
        return (
            <div style={{
                borderRadius:"3vw",
                width:"96%",
                margin:"2.551vw auto"
            }} onClick={ e=>this.toPolicyGuideLines(e)}  className={style.PolicyCard}>
            <div className={style.left}>
                <div className={style.title}>
                    <div className={style.text}>
                        {
                            policyItem.name
                        }
                    </div>
                    {
                        policyItem.isHot ?
                        <div className={style.icon}>
                        <img src={require("../../assect/image/declare_title_type.png")} alt="" />
                        </div> : ""
                    }
                    {
                        policyItem.cashId == 1 ?
                        <div className={style.icon}>
                        <img src={require("../../assect/image/declare_title_type2.png")} alt="" />
                        </div> : ""
                    }
                </div>
                <div className={style.synopsis}>
                    <div className={style.date}>
                        申报时间：{policyItem.startDate} - {policyItem.endDate}
                    </div>
                    <div className={style.dept}>
                        负责部门：{policyItem.unitName}
                    </div>
                </div>
                <div className={style.tags}>
                    {
                    (()=>{
                        let tags=["企业"];
                        if(policyItem.personal && policyItem.enterprise){
                            tags=["企业","个人"]
                        }else if(policyItem.personal && !policyItem.enterprise){
                            tags=["个人"]
                        }
                        return tags.map((item,index)=><div key={index} className={style.tag}>{item}</div>)
                        })()

                    }
                </div>
            </div>
            <div className={style.right}>
                <div onClick={e=>this.toPolicyGuideLines(e)} className={style.guide}>
                    <div className={style.icon}>
                        <img src={require("../../assect/image/book.png")} alt="" />
                    </div>
                    <div className={style.text}>
                        {
                            policyItem.cashId !=1 ? "申报指南" : "政策指南"
                        }
                    </div>
                </div>
                <div className={style.count}>
                    {
                        policyItem.cashId !=1 ? `当前申报次数：${policyItem.applyCount}` :""
                    }
                </div>
                <div onClick={ policyItem.cashId != 1 ? this.IwantApply : e=>this.toPolicyGuideLines}  style={{ background: policyItem.cashId !=1 ? "#398DF9" : "#44bd73"}} className={style.apply}>
                    {policyItem.cashId !=1 ? "我要申报" : "免申即享" }
                </div>
            </div>
            <div className={style.modal}>
                <Modal title="请选择申报类型" onCancel={(e)=>{
                    e.stopPropagation();
                    
                    this.setState({isVisible:false})
                }} onOk={()=>{console.log(999)}} footer={null} visible={isVisible} >
                    <div className={style.type_box}>
                        <div onClick={(e)=>{
                            e.stopPropagation();
                           try {
                            store.dispatch({type:"userChoose",content:"legal"})
                            if(localStorage.getItem("legalToken")){
                                console.log(111)
                                this.props.history.push({
                                    pathname:"apply_policy",
                                    state:{
                                        policy_id:policyItem.id,
                                        role:"personal"
                                    }
                                })
                            }else{
                                ssoLogin(ssoLoginType.legal)
                            }
                           } catch (error) {
                            console.log(error)
                           }
                        }} className={[style.type,style.enterprise].join(" ")}>
                            法人申请
                        </div>
                        <div onClick={async (e)=>{
                            e.stopPropagation();
                            if(localStorage.getItem("personalToken")){
                                this.props.history.push({
                                    pathname:"apply_policy",
                                    state:{
                                        policy_id:policyItem.id,
                                        role:"legal"
                                    }
                                })
                            }else{
                                ssoLogin(ssoLoginType.personal,"http://my.andbridge.com:3000/apply_policy");
                            }
                            
                        }} className={[style.type,style.personal].join(" ")}>
                            个人申请
                        </div>
                    </div>
                </Modal>
            </div>
            </div>
            
        )
    }
}